<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索区域 -->
    <el-card class="box-card">
      <el-row type="flex" justify="end" :gutter="20">
        <el-col :span="12">
          <el-input placeholder="请输入内容" prefix-icon="el-icon-search" v-model="queryData.query" clearable @keypress.enter.native="getGoodsList" @clear="getGoodsList">
            <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="handleAddGoods">添加商品</el-button>
        </el-col>
      </el-row>
    </el-card>
    <!-- 列表区域 -->
    <el-card class="listBox">
      <el-table :data="goodsList" style="width: 100%">
        <el-table-column prop="goods_id" label="商品编号" width="100">
        </el-table-column>
        <el-table-column prop="goods_name" label="商品名称" width="200" align="center">
        </el-table-column>
        <el-table-column prop="goods_price" label="商品价格 （元）" width="140" align="center">
        </el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="100" align="center">
        </el-table-column>
        <el-table-column label="添加时间" width="140" align="center">
          <template slot-scope="scope">
            {{$moment(scope.row.add_time*1000).format('YYYY/MM/DD')}}
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="small"></el-button>
            <el-button type="danger" icon="el-icon-delete" size="small" @click="handleDelete(scope.row)"></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 分页 -->
    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="queryData.pagenum" :page-sizes="[5, 10, 20, 30]" :page-size="queryData.pagesize" layout="total, sizes, prev, pager, next" :total="total">
    </el-pagination>
  </div>
</template>

<script>
import { getGoods, removeGoods } from '@/api/commodity.js'
export default {
  data () {
    return {
      queryData: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      total: 0,
      goodsList: []
    }
  },

  created () {
    this.getGoodsList()
  },

  methods: {
    // 获取商品列表
    async getGoodsList () {
      const { data: res } = await getGoods({
        query: this.queryData.query,
        pagenum: this.queryData.pagenum,
        pagesize: this.queryData.pagesize
      })
      console.log(res)
      if (res.meta.status !== 200) return this.$message.error('获取商品列表失败。')
      this.goodsList = res.data.goods
      this.total = res.data.total
    },
    // 分页相关
    handleSizeChange (val) {
      // 每页数量改变
      this.queryData.pagesize = val
      this.getGoodsList()
    },
    handleCurrentChange (val) {
      // 页码改变
      this.queryData.pagenum = val
      this.getGoodsList()
    },
    // 删除商品
    handleDelete (row) {
      console.log(row)
      this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const { data: res } = await removeGoods(row.goods_id)
        if (res.meta.status !== 200) return this.$message.error('删除失败。')
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        this.getGoodsList()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 添加商品
    handleAddGoods () {
      this.$router.push('/goods/add')
    }
  }
}
</script>

<style scoped lang='less'>
</style>
